#include "elk_cef_types_from.h"

QString ElkCefTypesFrom::requestFlags(cef_urlrequest_flags_t flags)
{
    if (flags == UR_FLAG_NONE) {
        return QString("none");
    }

    QString type_string;
    if (flags & UR_FLAG_SKIP_CACHE) {
        type_string.append("skip cache/");
    }
    if (flags & UR_FLAG_ONLY_FROM_CACHE) {
        type_string.append("only from cache/");
    }
    if (flags & UR_FLAG_DISABLE_CACHE) {
        type_string.append("disable cache/");
    }
    if (flags & UR_FLAG_ALLOW_STORED_CREDENTIALS) {
        type_string.append("allow stored credentials/");
    }
    if (flags & UR_FLAG_REPORT_UPLOAD_PROGRESS) {
        type_string.append("report upload progress/");
    }
    if (flags & UR_FLAG_NO_DOWNLOAD_DATA) {
        type_string.append("no download data/");
    }
    if (flags & UR_FLAG_NO_RETRY_ON_5XX) {
        type_string.append("no retry on 5xx/");
    }
    if (flags & UR_FLAG_STOP_ON_REDIRECT) {
        type_string.append("stop on redirect/");
    }
    if (type_string.endsWith("/")) {
        type_string.resize(type_string.length() - 1);
    }
    return type_string;
}

QString ElkCefTypesFrom::resourceType(cef_resource_type_t type)
{
    QString type_string;
    switch (type) {
    case RT_MAIN_FRAME:
        type_string = QString::fromLocal8Bit("main frame");
        break;
    case RT_SUB_FRAME:
        type_string = QString::fromLocal8Bit("sub frame");
        break;
    case RT_STYLESHEET:
        type_string = QString::fromLocal8Bit("css");
        break;
    case RT_SCRIPT:
        type_string = QString::fromLocal8Bit("script");
        break;
    case RT_IMAGE:
        type_string = QString::fromLocal8Bit("image");
        break;
    case RT_FONT_RESOURCE:
        type_string = QString::fromLocal8Bit("font resource");
        break;
    case RT_SUB_RESOURCE:
        type_string = QString::fromLocal8Bit("sub resource");
        break;
    case RT_OBJECT:
        type_string = QString::fromLocal8Bit("object");
        break;
    case RT_MEDIA:
        type_string = QString::fromLocal8Bit("media");
        break;
    case RT_WORKER:
        type_string = QString::fromLocal8Bit("worker");
        break;
    case RT_SHARED_WORKER:
        type_string = QString::fromLocal8Bit("shared worker");
        break;
    case RT_PREFETCH:
        type_string = QString::fromLocal8Bit("prefetch");
        break;
    case RT_FAVICON:
        type_string = QString::fromLocal8Bit("favicon");
        break;
    case RT_XHR:
        type_string = QString::fromLocal8Bit("xhr");
        break;
    case RT_PING:
        type_string = QString::fromLocal8Bit("ping");
        break;
    case RT_SERVICE_WORKER:
        type_string = QString::fromLocal8Bit("service worker");
        break;
    case RT_CSP_REPORT:
        type_string = QString::fromLocal8Bit("csp report");
        break;
    case RT_PLUGIN_RESOURCE:
        type_string = QString::fromLocal8Bit("plugin resource");
        break;
    default:
        break;
    }
    return type_string;
}

QString ElkCefTypesFrom::transitionType(cef_transition_type_t type)
{
    QString type_string;
    switch (type) {
    case TT_LINK:
        type_string = QString::fromLocal8Bit("link/");
        break;
    case TT_EXPLICIT:
        type_string = QString::fromLocal8Bit("explicit/");
        break;
    case TT_AUTO_SUBFRAME:
        type_string = QString::fromLocal8Bit("auto subframe/");
        break;
    case TT_MANUAL_SUBFRAME:
        type_string = QString::fromLocal8Bit("manual subframe/");
        break;
    case TT_FORM_SUBMIT:
        type_string = QString::fromLocal8Bit("form submit/");
        break;
    case TT_RELOAD:
        type_string = QString::fromLocal8Bit("reload/");
        break;
    default:
        break;
    }
    if (type & TT_QUALIFIER_MASK & TT_BLOCKED_FLAG) {
        type_string.append("blocked/");
    }
    if (type & TT_QUALIFIER_MASK & TT_FORWARD_BACK_FLAG) {
        type_string.append("forward back/");
    }
    if (type & TT_QUALIFIER_MASK & TT_DIRECT_LOAD_FLAG) {
        type_string.append("direct load/");
    }
    if (type & TT_QUALIFIER_MASK & TT_CHAIN_START_FLAG) {
        type_string.append("chain start/");
    }
    if (type & TT_QUALIFIER_MASK & TT_CHAIN_END_FLAG) {
        type_string.append("chain end/");
    }
    if (type & TT_QUALIFIER_MASK & TT_CLIENT_REDIRECT_FLAG) {
        type_string.append("client redirect/");
    }
    if (type & TT_QUALIFIER_MASK & TT_SERVER_REDIRECT_FLAG) {
        type_string.append("server redirect/");
    }
    if (type_string.endsWith("/")) {
        type_string.resize(type_string.length() - 1);
    }
    return type_string;
}

QString ElkCefTypesFrom::contextMenuType(cef_context_menu_type_flags_t flags)
{
    if (flags == CM_TYPEFLAG_NONE) {
        return QString("none");
    }

    QString type_string;
    if (flags & CM_TYPEFLAG_PAGE) {
        type_string.append("page/");
    }
    if (flags & CM_TYPEFLAG_FRAME) {
        type_string.append("frame/");
    }
    if (flags & CM_TYPEFLAG_LINK) {
        type_string.append("link/");
    }
    if (flags & CM_TYPEFLAG_MEDIA) {
        type_string.append("media/");
    }
    if (flags & CM_TYPEFLAG_SELECTION) {
        type_string.append("selection/");
    }
    if (flags & CM_TYPEFLAG_EDITABLE) {
        type_string.append("editable/");
    }
    if (type_string.endsWith("/")) {
        type_string.resize(type_string.length() - 1);
    }
    return type_string;
}

QString ElkCefTypesFrom::contextMenuMediaType(cef_context_menu_media_type_t type)
{
    QString type_string;
    switch (type) {
    case CM_MEDIATYPE_NONE:
        type_string = QString::fromLocal8Bit("none");
        break;
    case CM_MEDIATYPE_IMAGE:
        type_string = QString::fromLocal8Bit("image");
        break;
    case CM_MEDIATYPE_VIDEO:
        type_string = QString::fromLocal8Bit("video");
        break;
    case CM_MEDIATYPE_AUDIO:
        type_string = QString::fromLocal8Bit("audio");
        break;
    case CM_MEDIATYPE_FILE:
        type_string = QString::fromLocal8Bit("file");
        break;
    case CM_MEDIATYPE_PLUGIN:
        type_string = QString::fromLocal8Bit("plugin");
        break;
    default:
        break;
    }
    return type_string;
}

void ElkCefTypesFrom::fromRequest(CefRefPtr<CefRequest> request, ElkRequestDetails &details)
{
    details.url = request->GetURL().ToString().c_str();
    details.method = request->GetMethod().ToString().c_str();
    details.referrerUrl = request->GetReferrerURL().ToString().c_str();
    details.requestFlags = requestFlags((cef_urlrequest_flags_t)request->GetFlags());
    details.resourceType = resourceType(request->GetResourceType());
    details.transitionType = transitionType(request->GetTransitionType());
}
